package samsung.sds.business.vdiManagementSystem.systemManagement.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import samsung.sds.business.vdiManagementSystem.systemManagement.model.SystemInfoEntity;
import samsung.sds.business.vdiManagementSystem.systemManagement.service.SystemInfoService;
import samsung.sds.system.common.baseQuery.model.PageData;
import samsung.sds.system.common.model.QueryParam;
import samsung.sds.system.utils.CommonTools;
import samsung.sds.system.utils.Constants;
/**
 * 
 * @author litongwei
 * 
 */
@Controller
@RequestMapping("/vdiSystemInfo")
public class SystemInfoController {

	@Autowired
	private SystemInfoService systemInfoServiceImpl;
	
	private final static String URL = "/business/vdiManagementSystem/systemManagement/";

	@RequestMapping(value = "/vdiSystemInfoDisplay")
	public String vdiSystemInfoDisplay(HttpServletRequest request,
			ModelMap modelMap) {
		String pageType = request.getParameter("pageType");
		modelMap.addAttribute("pageType", pageType);
		return URL+"vdiSystemInfoList";
	}
	
	@RequestMapping(value = "/vdiSystemInfoDisplay_sitemesh_popup", method = RequestMethod.GET)
	public String vdiSystemInfoDisplayPop(HttpServletRequest request,
			ModelMap modelMap) {
		String pageType = request.getParameter("pageType");
		modelMap.addAttribute("pageType", pageType);
		return URL+"vdiSystemInfoListPopup";

	}

	@RequestMapping(value = "/createVdiSystemInfo")
	public String createVdiSystemInfo(HttpServletRequest request,
			ModelMap modelMap) {
		String username = request.getParameter("username");
		modelMap.addAttribute("username", username);
		modelMap.addAttribute("optype", "create");
		return URL+"addOrUpdateVdiSysInfo";
	}

	@RequestMapping(value = "/updateVdiSystemInfo")
	public String updateVdiSystemInfo(HttpServletRequest request, String id,
			ModelMap modelMap) {
		String CLOSE = "1";
		String vdiid = request.getParameter("id");
		String isClose = request.getParameter("isClose");
		if (CLOSE.equals(isClose)) {
			modelMap.addAttribute("isClose", isClose);
		}
		modelMap.addAttribute("optype", "update");
		modelMap.addAttribute("vdiid", vdiid);
		SystemInfoEntity entity = systemInfoServiceImpl
				.findEntityById(vdiid);
		modelMap.addAttribute("entity", entity);
		return URL+"addOrUpdateVdiSysInfo";
	}

	/**
	 * 修改或新增vdi系统信息
	 * 
	 * @param request
	 * @param model
	 * @param survey
	 * @return
	 */
	@RequestMapping(value = "/addOrUpdateVdiSysInfo", method = RequestMethod.POST)
	@ResponseBody
	public String addOrUpdateVdiSysInfo(HttpServletRequest request,
			SystemInfoEntity entity) {
		// 检查参数
		String opType = request.getParameter("optype");
		if (entity == null || opType == null) {
			return Constants.ERRMSG_PARAM_ERROR;
		}
		// 执行新建
		if (opType.equalsIgnoreCase("create")) {
			entity.setId(CommonTools.getKeyUUID());
			int rlt = systemInfoServiceImpl.insertEntity(entity);
			if (rlt == 1) {
				return Constants.SUCCESS_CODE;
			} else {
				return Constants.FAILURE_CODE;
			}
			// 执行修改
		} else if (opType.equalsIgnoreCase("update")) {
			int rlt = systemInfoServiceImpl.updateEntity(entity);
			if (rlt == 1) {
				return Constants.SUCCESS_CODE;
			} else {
				return Constants.FAILURE_CODE;
			}
		}
		// 参数不匹配则返回错误信息
		else {
			return Constants.ERRMSG_PARAM_ERROR;
		}
	}

	@RequestMapping(value = "/getVdiSystemInfoList")
	@ResponseBody
	public PageData<SystemInfoEntity> getVdiSystemInfoList(
			HttpServletRequest request) {
		QueryParam param = CommonTools.getQueryParam(request);
		PageData<SystemInfoEntity> pageData = systemInfoServiceImpl
				.queryAllVdiSysInfoByUser(param);
		return pageData;
	}
/**
 * 删除系统信息，删除成功后，将系统记录对应的IS_DELETE字段更新为1
 * @param request
 * @return
 * @throws UnsupportedEncodingException
 */
	@RequestMapping(value = "/deleteVdiSysInfo", method = RequestMethod.POST)
	@ResponseBody
	public String deleteVdiSysInfo(HttpServletRequest request) throws UnsupportedEncodingException {
		
		String idStr = request.getParameter("ids");
		String temUserName = URLDecoder.decode(idStr, "UTF-8");
		String[] ids = temUserName.split(",");
		List<String> list = new ArrayList<String>();

	    list = Arrays.asList(ids);
	    for(int i=0;i<list.size();i++){
	    	boolean flag = systemInfoServiceImpl.isUsed(list.get(i));
	    	if(flag){
				return "系统被分配VDI，请先解除与VDI的关联再进行删除！";
			}
	    }
	    int removeCount = systemInfoServiceImpl.updateSystemInfo(list);
	    if( removeCount > 0 ){
			return Constants.SUCCESS_CODE;
		}else{
			return Constants.FAILURE_CODE;
		}
	}

	@RequestMapping(value = "/queryVdiSysInfoDataByName")
	@ResponseBody
	public PageData<SystemInfoEntity> queryVdiSysInfoDataByName(
			HttpServletRequest request) {
		QueryParam param = CommonTools.getQueryParam(request);
		PageData<SystemInfoEntity> pageData = systemInfoServiceImpl
				.queryAllVdiSysInfoByUser(param);
		return pageData;
	}
}
